<?php
namespace app\controller;

use app\BaseController;
use think\App;
use think\facade\Db;

/**
 * Class Rank
 * @package app\controller
 *   mongodb 的小测试
 */
class Mongo extends BaseController
{
    private $mongo;
    const PREFIX = 'rank:';
    protected $connection = 'mongo';
    public function __construct(App $app)
    {
        $this->mongo = Db::connect('mongo');

        parent::__construct($app);
    }

    // 初始化
    public function init(){
        $goods = Db::name('goods')->select()->toArray();
        echo "<pre>";
        print_r($goods);
        #print_r($this->mongo);
        $this->mongo->table("goods")->insert(["goods"=>$goods]);
        $s = $this->mongo->table("goods")->select();
        print_r($s);
    }
    ///mongo更新
    public function update(){
        #$s = $this->mongo->table("goods")->where(['id'=>1])->find();
        $this->mongo->table("goods") ->where('goods.id', 1)
            ->update(['goods.name' => 'thinkphp']);
        $res = $this->mongo->table("goods")->select()->toArray();
        echo "<pre>";
        print_r($res);
        #foreach ($res){

        #}
    }


    ///mongo更新2
    public function update2(){
        echo "<pre>";
        $res = $this->mongo->table("goods")->select();
        $s = $this->mongo->table("goods")->where(['goods.name' => 'thinkphp8'])->find();
        print_r($s);
        $this->mongo->table("goods") ->where('_id', $s['_id'])
            ->update(['goods.count' => 2345]);


        print_r($res);
    }



}
